import { addDays, format } from 'date-fns';
import { initVueI18n } from '@dcloudio/uni-i18n';
import i18nMessages from '../locale';
import { isWechat } from './platform';

const { t } = initVueI18n(i18nMessages as any);

export function getDefaultBeforeDate() {
  return format(new Date(), 'yyyy-MM-dd');
}

export function getDefaultAfterDate(
  startDateStr: string = getDefaultBeforeDate(),
  addDaysNumber: number = 1
) {
  const startDate = new Date(startDateStr);
  const endDate = addDays(startDate, addDaysNumber);
  return format(endDate, 'yyyy-MM-dd');
}

export function getWeekday(current: string) {
  let weekdayNames = isWechat()
    ? ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    : [
        t('uni-calender.SUN'),
        t('uni-calender.MON'),
        t('uni-calender.TUE'),
        t('uni-calender.WED'),
        t('uni-calender.THU'),
        t('uni-calender.FRI'),
        t('uni-calender.SAT')
      ];
  const weekdayIndex = new Date(current).getDay();
  return weekdayNames![weekdayIndex];
}

export function getMinutesAndSeconds(seconds: number) {
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = seconds % 60;
  return { minutes, remainingSeconds };
}

export function formatTime(seconds: number) {
  const { minutes, remainingSeconds } = getMinutesAndSeconds(seconds);
  return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}

export function formatDateWithWeekday(date: string) {
  const weekday = getWeekday(date);
  return `${convertDateFormat(date)} ${weekday}`;
}

// 英文月份缩写
const monthAbbreviations = [
  'Jan',
  'Feb',
  'Mar',
  'Apr',
  'May',
  'Jun',
  'Jul',
  'Aug',
  'Sep',
  'Oct',
  'Nov',
  'Dec'
];

export function convertDateFormat(dateStr: string | Date) {
  if (typeof dateStr !== 'string') {
    dateStr = format(dateStr, 'yyyy-MM-dd');
  }

  // 拆分日期字符串并提取月和日
  const [, month, day] = dateStr.split('-');
  const parsedMonth = parseInt(month);
  const parsedDay = parseInt(day);

  const enMonthAbbr = monthAbbreviations[parsedMonth - 1];

  const translations = {
    'zh-Hant': `${parsedMonth}月${parsedDay}日`,
    ja: `${parsedMonth}月${parsedDay}日`,
    ko: `${parsedMonth}월 ${parsedDay}일`,
    en: `${enMonthAbbr} ${parsedDay}`,
    'zh-Hans': `${parsedMonth}月${parsedDay}日`
  };

  const locale = uni.getLocale() as keyof typeof translations;

  return translations[locale];
}

export function convertYearAndMonth(dateStr: string | Date) {
  if (typeof dateStr !== 'string') {
    dateStr = format(dateStr, 'yyyy-MM');
  }

  // 拆分日期字符串并提取年和月
  const [year, month] = dateStr.split('-');
  const parsedYear = parseInt(year);
  const parsedMonth = parseInt(month);

  const enMonthAbbr = monthAbbreviations[parsedMonth - 1];

  const translations = {
    'zh-Hant': `${parsedYear}年${parsedMonth}月`,
    ja: `${parsedYear}年${parsedMonth}月`,
    ko: `${parsedYear}년 ${parsedMonth}월`,
    en: `${enMonthAbbr} ${parsedYear}`,
    'zh-Hans': `${parsedYear}年${parsedMonth}月`
  };

  const locale = uni.getLocale() as keyof typeof translations;

  return translations[locale];
}
